package day_2022_7_to_8.day48;

/**
 * @author haomin
 * @date 2022/08/28 20:30
 **/
public class Day43_6_Num99_RecoverTree {
    class Solution {
        TreeNode first, second, pre;
        public void recoverTree(TreeNode root) {
            inOrder(root);
            int temp = first.val;
            first.val = second.val;
            second.val = temp;
        }

        private void inOrder(TreeNode root){
            if(root == null) return;

            inOrder(root.left);
            if(pre != null && root.val < pre.val){
                if(first == null) first = pre;
                second = root;
            }
            pre = root;
            inOrder(root.right);
        }
    }
}